package com.oracle.AopAnnotation;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAspect {

    //单独提取切入点表达式  公共的
    @Pointcut(value = "execution(* com.oracle.AopAnnotation.*.* (..))")
    public void pointcut(){}

    //前置通知
//    @Before(value = "pointcut()")
    public void before(JoinPoint joinPoint){
        System.out.println("开启事务");
    }

    //后置通知
//    @AfterReturning(value = "pointcut()",returning = "obj")
    public void afterReturning(Object obj){
        System.out.println("目标方法的返回值： "+obj);
    }
//
    @Around(value = "pointcut()")
    public Object around(ProceedingJoinPoint joinPoint){
        Object object = null;
        try {
            //前置通知
            before(joinPoint);

           object  = joinPoint.proceed(); //invoke返回对象

            //后置通知
            afterReturning(object);

        } catch (Throwable throwable) {
            //异常通知
            afterThrowing(throwable);
        }finally {
            //最终通知
            after();
        }
        return object;
    }
//
//    @AfterThrowing(value = "pointcut()",throwing = "e")
    public void afterThrowing(Throwable e){
        System.out.println("异常通知");
    }
//
//    @After(value = "pointcut()")
    public void after(){
        System.out.println("最终通知");
    }


}
